/****************************************************************************
 *
 * Copyright (C) 2003-2008 Los Alamos National Security, LLC
 *                         Packet Analytics Corporation
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License Version 2 as
 * published by the Free Software Foundation.  You may not use, modify or
 * distribute this program under any other version of the GNU General
 * Public License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 ****************************************************************************/
package nfse;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.Socket;

import javax.net.SocketFactory;

public class NumericIndexFileProcessor extends DataFileProcessor {

    /**********
     * This method connects to the Net/FSE server and sends record from the file
     * being processed to the server one-at-a-time for indexing.
     */
    public void process() {
        try {
            int offset = getOffset();
            if (offset > 0) {
                moveToOffset();
            }

            SocketFactory sf = SocketFactory.getDefault();
            Socket s = sf.createSocket(NetFSE.DATA_SERVER_HOST, NetFSE.DATA_SERVER_PORT);
            DataOutputStream dos = new DataOutputStream(s.getOutputStream());
            DataInputStream dis = new DataInputStream(s.getInputStream());
            dos.writeInt(getSensorID());
            dos.writeInt(1); // OP insert
            int reply = dis.readInt();
            if (reply == -1) {
                System.out.println("Sensor not supported (" + getSensorID() + ").");
                dos.close();
                dis.close();
                s.close();
                System.exit(1);
            }

            dos.writeInt(1);
            dos.writeInt(1);
            dos.writeInt(1);
            dos.writeInt(1);
            dos.writeInt(1);
            dos.writeInt(1);
            dos.writeInt(1);

            System.out.println("Processing " + getFilePath());

            byte[] bytes = null;
            while ((bytes = getNextRecord()) != null) {
                // System.out.println("BYTES: '" + new String(bytes) + "'");
                dos.writeInt(bytes.length);
                dos.write(bytes);
            }

            dos.writeInt(-1);
            dos.flush();
            dis.readInt();
            dos.close();
            dis.close();
            s.close();

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}
